<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use DB;
use App\Models\AdminRole;
use App\Models\Picture;
class User extends Model implements AuthenticatableContract
{
    protected $table = 'user';

    protected $primarykey = 'id';

    public $timestamps = false;
    protected $appends = array('mobile_picture');

    protected $fillable = ['user_name','real_name','sex','birthday','picture','password','phone','email','last_login_time','last_login_ip','is_disabled','point','remember_token','role_id','code','check_type','send_time'];
    function getMobilePictureAttribute(){

        $picture = $this->getAttribute('picture');
        if(!$picture){
            return null;
        }

        return Picture::find($picture)->url;


    }
    public function setPasswordAttribute($value){

    	$this->attributes['password'] = md5($value);

    }
    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier(){
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword(){
        return $this->password;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->{$this->getRememberTokenName()};
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->{$this->getRememberTokenName()} = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    /**
     * 是否拥有权限
     * @param $acl
     */
    public function hasPermission($acl){

        $adminRole = AdminRole::find($this->getAttribute('role_id'));

        if (!$adminRole) return false;

        $priv = $adminRole->role_priv;

        return  in_array($acl,$priv);
    }

}
